<?php

namespace component\element\ueditor\controller;

use app\bll\SyscnfUpload as BllSyscnfUpload;
use core\component\ElementBase;
use core\lib\upload\UploadImage;
use Exception;

use core\traits\ElementView as TraitsElementView;

class Index extends ElementBase
{

    public function index(...$param)
    {
        return $this->view($param);
    }

    public function upload()
    {
        $action =  request()->get('action', '');
        switch ($action) {
            case 'config':
                return json($this->ueconfig());
                break;

            case 'uploadimage':
                try {
                    $plugin = request()->elementinfo['plugin'] == '' ? 'frame'
                        : request()->elementinfo['plugin'];

                    $uploadImage = new UploadImage(
                        path: ($plugin),
                    );
                    $result = $uploadImage->upload();
                    return json([
                        "state"    => "SUCCESS",       //上传状态，上传成功时必须返回"SUCCESS"
                        "url"      => $result['path'],       //返回的地址
                        "original" => $result['orgname'],   //原始文件名
                        "size"     => $result['size'],       //文件大小
                    ]);
                } catch (Exception $ex) {
                    return json([
                        "state"    => "上传失败" . $ex->getMessage(),
                    ]);
                }


                break;
        }
    }

    public function ueconfig()
    {
        $bllSyscnfUpload = new BllSyscnfUpload();
        $cnfupload = $bllSyscnfUpload->find();
        return [
            /* 上传图片配置项 */
            'imageActionName' => 'uploadimage', //执行上传图片的action名称
            'imageFieldName' => 'uppic', //提交的图片表单名称
            'imageMaxSize' => $cnfupload['imagesize'] * 1024 * 1024, //上传大小限制，单位B
            "imageAllowFiles" => explode(',', $cnfupload['imageext']), /* 上传图片格式显示 */
            'imageCompressEnable' => false, //是否压缩图片,默认是false 
            'imageCompressBorder' => 900, //图片压缩最长边限制
            'imageInsertAlign' => 'none', //插入的图片浮动方式
            'imageUrlPrefix' => '', //图片访问路径前缀
            'imagePathFormat' => '', //上传保存路径上传保存路径

            /* 涂鸦图片上传配置项 */
            'scrawlActionName' => 'upscraw', //执行上传涂鸦的action名称
            'scrawlFieldName' => 'upscraw', //提交的图片表单名称
            'scrawlPathFormat' => '', //上传保存路径
            'scrawlMaxSize' => ' 6291456', //上传大小限制，单位B 6M
            'scrawlUrlPrefix' => '', //图片访问路径前缀
            'scrawlInsertAlign' => 'none', //插入的图片浮动方式


            /* 截图工具上传 */
            "snapscreenActionName" => '', /* 执行上传截图的action名称 */
            "snapscreenPathFormat" => 'uploadsnapscreen', /* 上传保存路径 */
            "snapscreenUrlPrefix" => '', /* 图片访问路径前缀 */
            "snapscreenInsertAlign" => 'none', /* 插入的图片浮动方式 */

            /* 上传视频配置 */
            "videoActionName" => 'uploadvideo', /* 执行上传视频的action名称 */
            "videoFieldName" => 'uploadvideo', /* 提交的视频表单名称 */
            "videoPathFormat" => '', /* 上传保存路径 */
            "videoUrlPrefix" => '', /* 视频访问路径前缀 */
            "videoMaxSize" => 2024000000,  /* 上传大小限制，单位B，默认100MB */
            'videoAllowFiles' => [
                ".flv",
                ".swf",
                ".mkv",
                ".avi",
                ".rm",
                ".rmvb",
                ".mpeg",
                ".mpg",
                ".ogg",
                ".ogv",
                ".mov",
                ".wmv",
                ".mp4",
                ".webm",
                ".mp3",
                ".wav",
                ".mid"
            ],

            /* 上传文件配置 */
            'fileActionName' => 'uploadfile',
            'fileFieldName' => 'uploadfile', //提交的文件表单名称
            'filePathFormat' => '', /* 上传保存路径 */
            'fileUrlPrefix' => '',/* 文件访问路径前缀 */
            'fileMaxSize' => '51200000',/* 上传大小限制，单位B，默认50MB */
            'fileAllowFiles' => [
                ".png",
                ".jpg",
                ".jpeg",
                ".gif",
                ".bmp",
                ".flv",
                ".swf",
                ".mkv",
                ".avi",
                ".rm",
                ".rmvb",
                ".mpeg",
                ".mpg",
                ".ogg",
                ".ogv",
                ".mov",
                ".wmv",
                ".mp4",
                ".webm",
                ".mp3",
                ".wav",
                ".mid",
                ".rar",
                ".zip",
                ".tar",
                ".gz",
                ".7z",
                ".bz2",
                ".cab",
                ".iso",
                ".doc",
                ".docx",
                ".xls",
                ".xlsx",
                ".ppt",
                ".pptx",
                ".pdf",
                ".md",
                ".xml"
            ],

            /* 列出指定目录下的图片 */
            "imageManagerActionName" => "listimage", /* 执行图片管理的action名称 */
            "imageManagerListPath" => "", /* 指定要列出图片的目录 */
            "imageManagerListSize" => 20, /* 每次列出文件数量 */
            "imageManagerUrlPrefix" => "", /* 图片访问路径前缀 */
            "imageManagerInsertAlign" => "none", /* 插入的图片浮动方式 */
            "imageManagerAllowFiles" => [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 列出的文件类型 */

            /* 列出指定目录下的文件 */
            "fileManagerActionName" => "listfile", /* 执行文件管理的action名称 */
            "fileManagerListPath" => "", /* 指定要列出文件的目录 */
            "fileManagerUrlPrefix" => "", /* 文件访问路径前缀 */
            "fileManagerListSize" => 20, /* 每次列出文件数量 */
            "fileManagerAllowFiles" => [
                ".png",
                ".jpg",
                ".jpeg",
                ".gif",
                ".bmp",
                ".flv",
                ".swf",
                ".mkv",
                ".avi",
                ".rm",
                ".rmvb",
                ".mpeg",
                ".mpg",
                ".ogg",
                ".ogv",
                ".mov",
                ".wmv",
                ".mp4",
                ".webm",
                ".mp3",
                ".wav",
                ".mid",
                ".rar",
                ".zip",
                ".tar",
                ".gz",
                ".7z",
                ".bz2",
                ".cab",
                ".iso",
                ".doc",
                ".docx",
                ".xls",
                ".xlsx",
                ".ppt",
                ".pptx",
                ".pdf",
                ".txt",
                ".md",
                ".xml"
            ], /* 列出的文件类型 */



            /* 抓取远程图片配置 */
            "catcherUrlPrefix" => '',
            "catcherActionName" => "catchimage", /* 执行抓取远程图片的action名称 */
            "catcherFieldName" => "source", /* 提交的图片列表表单名称 */
            "catcherMaxSize" => 20480000, /* 上传大小限制，单位B */
            "catcherAllowFiles" => [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 抓取图片格式显示 */
            "catchRemoteImageEnable" => true
        ];
    }
}
